﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Kernel.Connection;
using System.Windows.Threading;
using System.Globalization;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Media;
using static Kernel.Connection.GetPIDsConnectionInfo;

namespace CJFirewall.View
{
    /// <summary>
    /// ConnectionView.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectionView : UserControl
    {
        private GetPIDsConnectionInfo Info;
        public ConnectionView()
        {
            InitializeComponent();
            ReFresh();
            DispatcherTimer Timer = GetTimer();
            Timer.Interval = TimeSpan.FromSeconds(4);
            Timer.Tick += WelllcommeEnd;
            Timer.Start();
        }

        private static DispatcherTimer GetTimer()
        {
            return new DispatcherTimer();
        }

        void WelllcommeEnd(object sender, EventArgs e)
        {
            if(ISFresh.IsChecked==true&&this.IsVisible)
                ReFresh();
        }
        public void ReFresh()
        {
            if (Info == null)
                Info = new GetPIDsConnectionInfo();
            Info.GetInfo();
            Tip.Text = $"当前有{Info.ConnectionPIDNumber}个程序正在或曾经访问网络，共建立{Info.TcpProgressInfoTable.Length}个TCP连接, {Info.UdpProgressInfoTable.Length}个UDP连接";
            ConData.ItemsSource = Info.ConnectionInfoList;
        }

        private void Fresh_Click(object sender, RoutedEventArgs e)
        {
            ReFresh();
        }

        private void BtnMore_Click(object sender, RoutedEventArgs e)
        {
            ISFresh.IsChecked = false;
            var Info= (ConnectionInfo)((sender as Button).Content as System.Windows.Controls.Image).DataContext;
            var more = new ShowThis(Info);
            more.ShowDialog();
        }
    }
}
